﻿Imports MICROFORMA.BEL
Imports MICROFORMA.BLL

Public Class frmLists
    Dim listaBL As ListasBL

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        listaBL = New ListasBL

    End Sub

    Private Sub lnkNewList_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkNewList.LinkClicked
        Call ShowFormListAdd(False)
        Call LoadList()
    End Sub

    Private Sub ShowFormListAdd(Optional ByVal bolUpdate As Boolean = Nothing, Optional ByVal idLista As Short = 0)

        Dim listAdd As New frmListsAdd
        listAdd.bolUpdate = bolUpdate
        listAdd.idLista = idLista
        listAdd.ShowDialog()
        listAdd.Dispose()

    End Sub

    Private Sub frmLists_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call LoadList()
    End Sub

    Private Sub LoadList()
        lstvListData.Items.Clear()

        Dim _datableListas As DataTable = listaBL.List
        Dim lviItem As ListViewItem

        For Each dtrFila As DataRow In _datableListas.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdLista")
            lviItem.SubItems.Add(dtrFila("Nombre"))
            lviItem.ImageIndex = 1
            lstvListData.Items.Add(lviItem)
        Next

    End Sub

    Private Sub lnkEditList_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkEditList.LinkClicked
        If lstvListData.SelectedItems.Count > 0 Then
            Call ShowFormListAdd(True, CShort(lstvListData.SelectedItems(0).Text.Trim))
        End If
    End Sub

    Private Sub lnkDeleteList_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkDeleteList.LinkClicked
        If lstvListData.SelectedItems.Count > 0 Then
            Call DeleteDataExternal(lstvListData.SelectedItems(0).Text)
            Call LoadList()
        Else
            MessageMistake("Por favor Seleccione un Registro.")
            lstvListData.Focus()
        End If
    End Sub

    Private Sub DeleteDataExternal(ByVal idLista As Int16)
        If MessageMakeAction("Esta Seguro de eliminar el Registro Seleccionado.") Then
            Dim msg As String = listaBL.Delete(idLista)
            If Not msg = "OK" Then
                MessageMistake(msg)
            End If
        End If
    End Sub

End Class